package cn.tedu.baidashi.controller;

import cn.tedu.baidashi.model.Category;
import cn.tedu.baidashi.service.ICategoryService;
import cn.tedu.baidashi.vo.ItemsInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Resource
    private ICategoryService categoryService;

    @GetMapping
    public List<Category> getCategory(){
        System.out.println(".....");
        List<Category> categories = categoryService.getCategory();
        System.out.println(categories);
        return categories;
    }

    /**
     * 根据分类id查询商品
     * @param id
     * @return
     */
    @GetMapping("/items/{id}")
    public List<ItemsInfoVO> getItems(@PathVariable Integer id){
        log.debug("分类id为:{}",id);
        List<ItemsInfoVO> itemsInfoVOS = categoryService.getItemsById(id);
        System.out.println(itemsInfoVOS);
        return itemsInfoVOS;
    }

    /**
     * 根据分类id查询分类名称
     */
    @GetMapping("/{id}")
    public String getCategory(@PathVariable Integer id){
        System.out.println("分类id"+id);
        String category = categoryService.category(id);
        System.out.println(category);
        return category;
    }

    /**
     * 查询所有商品
     */
    @GetMapping("/items")
    public List<ItemsInfoVO> getItems(){
        log.debug("全部商品执行了");
        List<ItemsInfoVO> itemsInfoVOS = categoryService.findItems();
        return itemsInfoVOS;
    }
}
